package com.bigknow.appstore.web.util;

import javax.imageio.ImageIO;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;

/**
 * Created by 李科风 on 2016-10-08.
 */
public class ImageUtils {

    public static String getFormatName(File file) {
        try (ImageInputStream iis = ImageIO.createImageInputStream(file)) {
            Iterator<ImageReader> iter = ImageIO.getImageReaders(iis);
            if (!iter.hasNext()) {
                return null;
            }

            ImageReader reader = iter.next();
            return reader.getFormatName();
        } catch (IOException e) {
            throw new RuntimeException("读取文件失败，不可识别的图片格式");
        }
    }
}
